package com.example.service;

import java.io.Serializable;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * 公共sercice层
 * 
 * @author lli
 *
 * @version 1.0
 * 
 * @param <T>
 * @param <PK>
 */
public class BaseService<T, PK extends Serializable> {

    protected JpaRepository<T, PK> baseDao;

    public BaseService(JpaRepository<T, PK> dao) {
        this.baseDao = dao;
    }

    /** 具体操作  */

    public T findOne(PK id) {

        return baseDao.findOne(id);
    }
    
    public long count(PK id) {

        return baseDao.count();
    }
    
    public boolean exists(PK id) {
        return baseDao.exists(id);
    }

    public void save(T object) {
        baseDao.save(object);
    }

    public void delete(PK id) {
        baseDao.delete(id);
    }

    public List<T> findAll() {
        return baseDao.findAll();
    }

    @SuppressWarnings("unchecked")
    public void remove(PK... pks) {
        for (PK k : pks)
            baseDao.delete(k);
    }

    public Page<T> findAll(Pageable page) {
        return baseDao.findAll(page);
    }

    public void save(Iterable<T> entities) {
        baseDao.save(entities);
    }

}
